/**============================================================
 * 版权：Autohome 版权所有 (c) 2014 - 2015
 * 文件：com.athm.framework.dao.type.BooleanTypeHandler.java
 * 所含类:BooleanTypeHandler.java
 * 修改记录：
 * 日期                           作者                            内容
 * =============================================================
 * Jan 26, 2015 4:28:35 PM    forever       创建文件，实现基本功能
 * ============================================================*/
package com.quanyou.base.framework.dao.mybatis.type;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p> BooleanTypeHandler</p>
 * 
 * <p> 说明  </p>
 * 
 * <p> Copyright: 版权所有 (c) 2014 - 2015 </p>
 * <p> Company:Autohome </p>
 * 
 * @author forever
 * @version 1.0
 */
@MappedJdbcTypes(JdbcType.CHAR)
public class BooleanTypeHandler extends BaseTypeHandler<Boolean> {

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
		if(parameter!=null &&parameter){
			ps.setString(i, "Y");
		}else{
			ps.setString(i, "N");
		}
	}

	@Override
	public Boolean getNullableResult(ResultSet rs, String columnName) throws SQLException {
		String value = rs.getString(columnName);
		return value == "Y" ?true:false;
	}

	@Override
	public Boolean getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		String value = rs.getString(columnIndex);
		return value == "Y" ?true:false;
	}

	@Override
	public Boolean getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		String value = cs.getString(columnIndex);
		return value == "Y" ?true:false;
	}
}

